<?php

namespace App\Models\Prize;

use App\Models\Groupable;
use App\Models\Grouped;
use App\Support\Database\Eloquent\UuidModel;

class Prize extends UuidModel
{
    use Groupable;

    //奖品是否可用，0否、1是
    const IS_VALID_YES = 1;
    const IS_VALID_NO = 0;
    //奖品来源 1自建 2购买
    const BUILD_BY_CREATE = 1;
    const BUILD_BY_BUY = 2;
    //领取方式 0现场兑换 1 邮寄
    const SPOT_EXCHANGE = 0;
    const MAIL_POST = 1;
    //券码类型 0无 1统一码 2自定义csv
    const NO_VOUCHER = 0;
    const UNICODE_VOUCHER = 1;
    const CUSTOM_VOUCHER = 2;
    //奖品来源
    const RESOURCE_TYPES = array(
        '1' => '自建奖品',
        '2' => '平台购买'
    );
    //订单页显示项
    const LABEL_NAMES = array(
        'order_id' => '订单号',
        'order_name' => '订单名称',
        'goods_name' => '商品名称',
        'description' => '商品描述',
        'price' => '单价',
        'number' => '数量',
        'amount' => '总额',
    );

    protected $fillable = [
        'user_id',
        'type_code',
        'name',
        'expired_start',
        'expired_end',
        'stock_number',
        'tag',
        'background_image',
        'exchange_method',
        'exchange_address',
        'voucher_type',
        'voucher_code',
        'voucher_link',
        'exchange_url',
        'resource',
        'is_valid',
        'group_code',
        'extends',

    ];

    protected $guarded = array('created_at', 'updated_at');

    public function prizeType()
    {
        return $this->belongsTo('\App\Models\PrizeType\PrizeType', 'type_code', 'code');
    }

    public function sponsor()
    {
        return $this->hasMany('\App\Models\Sponsor\Sponsor', 'prize_id', 'id');
    }

    public function activity()
    {
        return $this->belongsToMany('\App\Models\ActivityInfo\ActivityInfo', 'activity_prizes', 'prize_id', 'activity_id');
    }

    public function prizeRecord()
    {
        return $this->hasMany('\App\Models\PrizeRecord\PrizeRecord', 'prize_id', 'id');
    }

    public function prizePayment()
    {
        return $this->hasOne('\App\Models\PrizePayment\PrizePayment', 'prize_id', 'id');
    }

    public function isValid()
    {
        return $this->is_valid == self::IS_VALID_YES;
    }

    public function setIsValid()
    {
        $this->is_valid = self::IS_VALID_YES;

        return $this->save();
    }
}
